﻿
using DAL;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace BEL
{
    public class Rubro
    {
        #region atributos
        private int _rubId;
        private String _rubNombre;       
        #endregion

        #region properties
        public int RubId
        {
            get { return _rubId; }
            set { _rubId = value; }
        }
        
        public String RubNombre
        {
            get { return _rubNombre; }
            set { _rubNombre = value; }
        }
       
        #endregion

        #region constructores
        public Rubro()
        {

        }

        public Rubro(int rubroId,String rubroNombre)
        {
            _rubId = rubroId;
            _rubNombre = rubroNombre;
            
        }

        #endregion

        #region funciones
        public Rubro BuscarRubroPorId(int RubroId)
        {
            Rubro rubro = new Rubro();
            SqlDataReader dReader = new DALRubros().BuscarRubroPorId(RubroId);

            while (dReader.Read())
            {
                rubro = LoadRubro(dReader);
            }
            dReader.Close();
            return rubro;

        }
        public bool AltaRubro(string nombre)
        {
            this.RubNombre = nombre;
            if (DALRubros.AltaRubro(this.toHashtable()) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool ModificarRubro(int id, string nombre)
        {
            this.RubId = id;
            this.RubNombre = nombre;
            if (DALRubros.ModificarRubro(this.toHashtable()) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }


        private static Rubro LoadRubro(IDataReader dReader)
        {

            Rubro rubroEntity = new Rubro();
            //paisEntity.id = (DBNull.Value == dReader["id"]) ? 0 : (int)dReader["id"];
            rubroEntity.RubId = (DBNull.Value == dReader["RubId"]) ? 0 : (int)dReader["RubId"];
            rubroEntity.RubNombre = (DBNull.Value == dReader["RubNombre"]) ? string.Empty : (string)dReader["RubNombre"];

            return rubroEntity;
        }

        private Hashtable toHashtable()
        {
            Hashtable hash = new Hashtable();

            if (!string.IsNullOrEmpty(RubId.ToString()))
            {
                hash.Add("RubId", this.RubId);
            }
            hash.Add("RubNombre", this.RubNombre);
            return hash;

        }
        private void fromHashtable(Hashtable ht)
        {

            this.RubId = Convert.ToInt32(ht["RubId"].ToString());
            this.RubNombre = ht["RubNombre"].ToString();
          
        }

        public List<Rubro> BuscarRubros()
        {
            List<Rubro> pCollection = new List<Rubro>();
            SqlDataReader dReader = DALRubros.BuscarRubros();

            while (dReader.Read())
            {
                Rubro rub = LoadRubro(dReader);
                pCollection.Add(rub);
            }
            dReader.Close();
            return pCollection;
        }

        public static List<Rubro> BuscarRubrosXComercio(int comercioid) {
            List<Rubro> retorno = new List<Rubro>();
            SqlDataReader dr = DALRubros.BuscarRubrosXComercio(comercioid);

            while(dr.Read()){
                Rubro r = new Rubro();
                r.RubId = Convert.ToInt32(dr["RubId"]);
                r.RubNombre = Convert.ToString(dr["RubNombre"]);
                retorno.Add(r);
            } 
            
            return retorno;
        }
    }
    #endregion

}
